package com.romotc.qqrobot.webqq.qqInterfaceCall;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.romotc.qqrobot.bean.QQLogedin;
import com.romotc.qqrobot.webqq.bean.Add_need_verify2_RequestBody;
import com.romotc.qqrobot.webqq.bean.Add_need_verify2_RequestBody_R;
import com.romotc.qqrobot.webqq.bean.Search_qq_by_uin2_ResponseBody;
import com.romotc.qqrobot.webqq.qqInterfaceCall.dataProcessor.Search_qq_by_uin2_Processor;
import com.romotc.scache.httpreq;
/**
 * 调用http://s.web2.qq.com/api/add_need_verify2接口加好友
 * @author Administrator
 *正常的返回：{"retcode":0,"result":{"result":0}}
 *请求数据格式错误：{"retcode":100001}
 *请求过于频繁：{"retcode":100003} 对任何号码的加好友申请都会屏蔽
 */
public class Add_need_verify2_InterCall extends QQInterfaceCall {
	private static final String reqUrl = "http://s.web2.qq.com/api/add_need_verify2";
	private static final int req_method = httpreq.REQUEST_POST;
	
	/**
	 * 必传参数:要加的好友主号码
	 */
	public static final String Key_Input_Tuin = Search_qq_by_uin2_InterCall.Key_Input_Tuin;
	
	/**
	 * 入参:将好友加到哪个组 默认0
	 */
	public static final String Key_Input_GroupId = "add2GroupId";
	
	/**
	 * 入参：请求验证信息
	 */
	public static final String Key_Input_ReqFriendMsg = "ReqFriendMsg";
	public String Default_ReqFriendMsg = "业务合作";	
	
	@Override
	public JSONObject preCall(QQLogedin loginQQ, JSONObject reqDataObj)
			throws Exception {
		Search_qq_by_uin2_InterCall call = new Search_qq_by_uin2_InterCall();
		
		JSONObject jsonRtInfo = call.call(loginQQ, 
				reqDataObj, Search_qq_by_uin2_Processor.getInstance());
		
		
		return jsonRtInfo;
	}
	
	
	@Override
	public ArrayList<String> myReqHeaders(int reqBodyLen, QQLogedin loginQQ,JSONObject reqDataObj) {
		
		ArrayList<String> s = buildReqHeaders(reqBodyLen, loginQQ,
				"http://s.web2.qq.com/proxy.html?v=20110412001&callback=1&id=2", 
				"s.web2.qq.com", "http://s.web2.qq.com");
		//调用后返回{"retcode":100001}问题
		s.add("Content-Type: application/x-www-form-urlencoded");
		return s;
	}
	
	@Override
	public String buildReqBody(JSONObject reqDataObj, QQLogedin loginQQ) throws Exception{
		
		if(reqDataObj != null) {
			Search_qq_by_uin2_ResponseBody uin;
			
			uin = (Search_qq_by_uin2_ResponseBody)reqDataObj.get(Search_qq_by_uin2_Processor.Key_Rt_Search_qq_by_uin2_ResponseBody);
			//long tuin = uin.getResult().getTuin();
			
			/**
			http://s.web2.qq.com/api/search_qq_by_uin2?tuin=13361004869
				{"retcode":0,
					"result":{"birthday":{"month":2,"year":1985,"day":23},"face":579,
								"phone":"","allow":4,"college":"","city":"","uiuin":"13361004869",
								"personal":"","shengxiao":0,
								"token":"bac641367d84063b39e9d3f92a40334485556bc85446ea3d",
								"client_type":1,"province":"","gender":"male",
								"occupation":"","reg_time":0,"blood":0,"constel":0,"homepage":"",
								"stat":10,"country":"中国","nick":"唐荣军","email":"","account":2761004869,
								"tuin":1587132593,"mobile":"133********"
							 }
				}

			http://s.web2.qq.com/api/add_need_verify2 请求体
				r:{"account":2761004869,"myallow":1,"groupid":0,"msg":"ddd",
					"token":"bac641367d84063b39e9d3f92a40334485556bc85446ea3d",
					"vfwebqq":"fc71a50e8187d4385af3aad55a09f19f8b26edb925ed7c28b7d85e1fab1372c313f2c02393a9b3bb"
				  }
				  
				  
			 */
			
			Add_need_verify2_RequestBody_R r = new Add_need_verify2_RequestBody_R();
			r.setAccount(uin.getResult().getAccount());
			r.setMyallow(1);
			r.setGroupid(reqDataObj.optInt(Key_Input_GroupId, 0));
			r.setMsg(reqDataObj.optString(Key_Input_ReqFriendMsg, Default_ReqFriendMsg ));
			r.setToken(uin.getResult().getToken());
			r.setVfwebqq(loginQQ.getVfwebqq());
			
			Add_need_verify2_RequestBody body = new Add_need_verify2_RequestBody();
			body.setR(r);
			
			return body.toString();

			
		}
		throw new Exception("前置请求失败,reqDataObj为空");
	}

	@Override
	public int getLogLevel() {
		return 1001;
	}

	@Override
	public String getReqUrl(JSONObject reqDataObj, QQLogedin loginQQ)
			throws JSONException {
		return reqUrl;
	}

	@Override
	public int getReq_method() {
		return req_method;
	}


	private static Add_need_verify2_InterCall instance;
	public static synchronized Add_need_verify2_InterCall getInstance() {
		if(instance == null)
			instance = new Add_need_verify2_InterCall();
		return instance;
	}


	@Override
	public boolean hasPreCall(QQLogedin loginQQ, JSONObject reqDataObj) {
		// TODO Auto-generated method stub
		return true;
	}


}
